/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using DaveSexton.DocProject.Sandcastle.TopicManagement;
using System.Xml;

namespace DaveSexton.DocProject.Sandcastle.ContentManagement
{
	[Serializable]
	public sealed class XmlCommentSectionContentItemAttributeData : ContentItemAttributeData		// Attribute is a reserved suffix, so Data was appended
	{
		#region Public Properties
		public override TopicSectionAttributeType AttributeType
		{
			get
			{
				return attributeType;
			}
		}

		public string Section
		{
			get
			{
				return section;
			}
		}

		public override string Value
		{
			get
			{
				return base.Value;
			}
			set
			{
				base.Value = value;
			}
		}
		#endregion

		#region Private / Protected
		private readonly string section;
		[NonSerialized]
		private readonly XmlElement element;
		private readonly TopicSectionAttributeType attributeType;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="XmlCommentSectionContentItemAttributeData" /> class.
		/// </summary>
		public XmlCommentSectionContentItemAttributeData(ContentItem owner, string section, XmlElement element)
			: base(owner)
		{
			if (string.IsNullOrEmpty(section))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "section");

			if (element == null)
				throw new ArgumentNullException("element");

			this.section = section;
			this.element = element;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="XmlCommentSectionContentItemAttributeData" /> class.
		/// </summary>
		public XmlCommentSectionContentItemAttributeData(ContentItem owner, string section, string name, XmlElement element, ITopicSectionAttributeData attribute)
			: this(owner, section, name, (element == null) ? null : element.GetAttribute(name), element, attribute)
		{
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="XmlCommentSectionContentItemAttributeData" /> class.
		/// </summary>
		public XmlCommentSectionContentItemAttributeData(ContentItem owner, string section, string name, string value, XmlElement element, ITopicSectionAttributeData attribute)
			: base(owner, name)
		{
			if (string.IsNullOrEmpty(section))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "section");

			if (element == null)
				throw new ArgumentNullException("element");

			if (attribute == null)
				throw new ArgumentNullException("attribute");

			this.section = section;
			this.element = element;

			attributeType = attribute.AttributeType;

			if (!string.IsNullOrEmpty(value))
				Value = value;
		}
		#endregion

		#region Methods
		public void CommitChanges()
		{
			XmlAttribute attribute = element.Attributes[Name];

			string value = Value;

			if (value == null)
			{
				if (attribute != null)
					element.Attributes.Remove(attribute);
			}
			else
			{
				if (attribute == null)
					attribute = element.Attributes.Append(element.OwnerDocument.CreateAttribute(Name));

				attribute.Value = value;
			}
		}

		public void CancelChanges()
		{
			Value = element.GetAttribute(Name);
		}
		#endregion
	}
}
